package com.wk.paradise.mkdw.controller;

import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Classname UploadController
 * @Description TODO
 * @Date 2020/1/15 15:23
 * @Author by weikai
 */
@Controller
public class UploadController {

  private final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

  @PostMapping("/upload")
  @ResponseBody
  public String upload(@RequestParam("file") MultipartFile file) {
    if (file.isEmpty()) {
      return "上传失败，请选择文件";
    }

    String fileName = file.getOriginalFilename();
    String filePath = "/tmp/";
    File dest = new File(filePath + fileName);
    try {
      StopWatch stopWatch = new StopWatch();
      stopWatch.start();
      file.transferTo(dest);
      stopWatch.stop();
      LOGGER.info("数据的大小为：{}\n，",stopWatch.prettyPrint());
      LOGGER.info("上传成功");
      return "上传成功";
    } catch (IOException e) {
      LOGGER.error(e.toString(), e);
    }
    return "上传失败！";
  }
}
